local DIY = require "packages.diy_utility.diy_utility"
local U = require "packages.utility.utility"

local skel = fk.CreateSkill {
  name = "emo__tonghui",
  tags = {DIY.ReadySkill},
}

Fk:loadTranslationTable{
  ["emo__tonghui"] = "统麾",
  [":emo__tonghui"] = "蓄势技，出牌阶段结束时，你可以将手牌数至多调整至体力数，然后令所有其他角色各蓄谋等量牌。",
  -- 至多调整至体力数：体力值为2时，可选0，1，2；为所有其他角色各蓄谋量牌是我方选牌

  ["#emo__tonghui-ask"] = "统麾：你可以将手牌数调整至至多为 %arg",
  ["#emo__tonghui-choice"] = "统麾：选择要将手牌数调整至的张数",
  ["#emo__tonghui-xumou"] = "统麾：请蓄谋你1张牌",

  ["$emo__tonghui1"] = "江东之水成潮，必没江准、摧幽冀、湮雍凉！",
  ["$emo__tonghui2"] = "今日授汝天子剑，且以敌祭杀贼戈。",
}

skel:addEffect(fk.EventPhaseEnd, {
  anim_type = "big",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player.phase == Player.Play and player.hp >= 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local hp = player.hp
    if not room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#emo__tonghui-ask:::"..hp }) then return end
    local canDiscard = #table.filter(player.player_cards[Player.Hand], function (id)
      return not player:prohibitDiscard(id)
    end)
    local hand = player:getHandcardNum()
    local choices = {}
    for i = hand - canDiscard, hand - 1, 1 do
      table.insert(choices, i)
    end
    for i = hand+1, hp, 1 do
      table.insert(choices, i)
    end
    choices = table.map(choices, tostring)
    table.insert(choices, "Cancel")
    local choice = room:askToChoice(player, {
      skill_name = skel.name, choices = choices, prompt = "#emo__tonghui-choice"
    })
    local num = tonumber(choice)
    if not num then return end
    event:setCostData(self, { num = num })
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = event:getCostData(self).num
    local changeNum = num - player:getHandcardNum()
    if changeNum < 0 then
      changeNum = math.abs(changeNum)
      room:askToDiscard(player, {
        skill_name = skel.name, min_num = changeNum, max_num = changeNum, cancelable = false, include_equip = false,
      })
    elseif changeNum > 0 then
      player:drawCards(changeNum, skel.name)
    end
    if changeNum == 0 then return end
    for _, to in ipairs(room:getOtherPlayers(player)) do
      if player.dead then break end
      for _ = 1, changeNum do
        if to:isNude() then break end
        local cid = room:askToChooseCard(to, { skill_name = skel.name, flag = "he", target = to, prompt = "#emo__tonghui-xumou" })
        U.premeditate (to, cid, skel.name)
      end
    end
  end,
})


--[[
skel:addEffect("active", {
  anim_type = "big",
  interaction = function (self, player)
    return UI.Spin{ from = 0, to = math.max(0, player.hp) }
  end,
  prompt = function (self, player)
    return "#emo__tonghui-ask:::"..player.hp
  end,
  target_num = 0,
  card_num = 0,
  can_use = function (self, player)
    return player.phase == Player.Play and player.hp >= 0
  end,
  on_use = function (self, room, skillUseEvent)
    local player = skillUseEvent.from
    local num = skillUseEvent.interaction_data
    if not num then return end
    local changeNum = num - player:getHandcardNum()
    if changeNum < 0 then
      changeNum = math.abs(changeNum)
      room:askToDiscard(player, {
        skill_name = skel.name, min_num = changeNum, max_num = changeNum, cancelable = false, include_equip = false,
      })
    elseif changeNum > 0 then
      player:drawCards(changeNum, skel.name)
    end
    if changeNum == 0 then return end
    for _, to in ipairs(room:getOtherPlayers(player)) do
      if player.dead then break end
      for _ = 1, changeNum do
        if to:isNude() then break end
        local cid = room:askToChooseCard(to, { skill_name = skel.name, flag = "he", target = to, prompt = "#emo__tonghui-xumou" })
        U.premeditate (to, cid, skel.name)
      end
    end
  end,
})

--]]

return skel
